package com.ewhl.mark.meijialian;

import android.content.Context;
import android.graphics.Color;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.bumptech.glide.Glide;
import com.ewhl.mark.meijialian.volley.BaseVO;
import com.ewhl.mark.meijialian.volley.MyErrorListener;
import com.ewhl.mark.meijialian.volley.MyReponseListener;
import com.zhy.autolayout.AutoLinearLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ShopAdapter extends BaseAdapter {
    Context context;
    List<ShopBean.ListBean> mdata;
    NetValues netValues;
    CollectionListner collectionListner;
    public ShopAdapter(Context context) {
        this.context = context;
        mdata = new ArrayList<>();
        netValues=NetValues.getInstance(context);
    }

    public CollectionListner getCollectionListner() {
        return collectionListner;
    }

    public void setCollectionListner(CollectionListner collectionListner) {
        this.collectionListner = collectionListner;
    }

    @Override
    public int getCount() {
        return mdata.size();
    }

    @Override
    public Object getItem(int position) {
        return mdata.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    public List<ShopBean.ListBean> getMdata() {
        return mdata;
    }

    public void setMdata(List<ShopBean.ListBean> mdata) {
        this.mdata = mdata;
        notifyDataSetChanged();
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_shop, parent, false);
            viewHolder=new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder=(ViewHolder) convertView.getTag();
        }
        Glide.with(context).load(Uri.parse(mdata.get(position).getStore_logo())).into(viewHolder.ivHeader);
        viewHolder.tvName.setText(mdata.get(position).getStore_name());
        viewHolder.llTips.setText(mdata.get(position).getSc_name());
        viewHolder.tvDis.setText(mdata.get(position).getDis());
        viewHolder.shopCollection.setBackgroundResource(R.drawable.shop_collection);
        if(mdata.get(position).getIs_shoucang()==1){
            viewHolder.shopCollection.setBackgroundResource(R.drawable.shop_collected);
            viewHolder.ivStar.setVisibility(View.GONE);
            viewHolder.tvCollect.setText("已收藏");
            viewHolder.tvCollect.setTextColor(Color.parseColor("#FFFFFF"));
        }else {
            viewHolder.shopCollection.setBackgroundResource(R.drawable.shop_collection);
            viewHolder.ivStar.setVisibility(View.VISIBLE);
            viewHolder.tvCollect.setText("收藏");
            viewHolder.tvCollect.setTextColor(Color.parseColor("#7dbbfa"));
        }
        viewHolder.shopCollection.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                collectionListner.collected(position,mdata.get(position).getIs_shoucang());

            }
        });
        return convertView;
    }

    static class ViewHolder {
        @BindView(R.id.iv_header)
        CircleImageView ivHeader;
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.ll_tips)
        TextView llTips;
        @BindView(R.id.iv_star)
        ImageView ivStar;
        @BindView(R.id.tv_collect)
        TextView tvCollect;
        @BindView(R.id.shop_collection)
        AutoLinearLayout shopCollection;
        @BindView(R.id.tv_dis)
        TextView tvDis;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
   public interface CollectionListner{
        void collected(int position,int is_shoucang);
    }
}
